/*
 * @Author: 230800029 rixing.liu@hytera.com
 * @Date: 2024-04-19 11:41:32
 * @LastEditors: 230800029 rixing.liu@hytera.com
 * @LastEditTime: 2024-04-19 14:50:32
 * @FilePath: /vue3-pg/src/store/modules/todolist/index.ts
 * @Description:
 * Copyright (c) 2024 by rixing.liu, All Rights Reserved.
 */
import { defineStore } from "pinia";
import { TodoItem, TodoListState } from "./type";
import { uniqueId } from "lodash-es";

export default defineStore("todolist", {
  state: (): TodoListState => ({
    list: [
      { name: "吃饭", id: "1", done: true },
      { name: "睡觉", id: "2", done: false },
      { name: "打豆豆", id: "3", done: false },
    ],
  }),
  actions: {
    addOne(one: TodoItem) {
      one.id = uniqueId();
      this.list.push(one);
    },
    deleteOne(id: string) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    deleteAll() {
      this.list = [];
    },
    doneAll() {
        this.list.forEach((item) => {
          item.done = true;
        });
    },
    updateOne(id: string, newItem: Partial<TodoItem>) {
      const index = this.list.findIndex((item) => item.id === id);
      if (index !== -1) {
        this.list[index] = { ...this.list[index], ...newItem };
      }
    },
  },
});
